package com.cskaoyan.service;

import com.cskaoyan.bean.bo.front.order.OrderCommentBo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.front.order.GoodsListVo;
import com.cskaoyan.bean.vo.front.order.OrderVo;
import com.cskaoyan.bean.vo.ordervo.FrontOrderInfoVo;
import com.cskaoyan.bean.vo.ordervo.FrontOrderInfoVo2;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.*;

/**
 * @ClassName:FrontOrderServiceImpl
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/13 23:09
 */
@Service
public class FrontOrderServiceImpl implements FrontOrderService {

    private Short REFUND_STATUS = 202;
    private Short CONFIRM_STATUS = 401;
    private Short PREPAY_STATUS = 201;
    private Short CANCEL_STATUS = 102;
    private Date date = new Date();

    @Autowired
    FrontOrderMapper frontOrderMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Override
    public CommonData list(BaseParam baseParam, Integer showType, Integer userId) {

        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);
        List<MarketOrder> marketOrders = null;
        if (showType == 0) {
            marketOrders = marketOrderMapper.selectByExample(example);
        }
        if (showType == 1) {
            Short[] shorts = {101};
            criteria.andOrderStatusIn(Arrays.asList(shorts));
            marketOrders = marketOrderMapper.selectByExample(example);
        }
        if (showType == 2) {
            Short[] shorts = {201};
            criteria.andOrderStatusIn(Arrays.asList(shorts));
            marketOrders = marketOrderMapper.selectByExample(example);
        }
        if (showType == 3) {
            Short[] shorts = {301};
            criteria.andOrderStatusIn(Arrays.asList(shorts));
            marketOrders = marketOrderMapper.selectByExample(example);
        }
        if (showType == 4) {
            Short[] shorts = {401};
            criteria.andOrderStatusIn(Arrays.asList(shorts));
            marketOrders = marketOrderMapper.selectByExample(example);
        }
        //101待付款、201已付款、301已发货、401用户收货
        List<OrderVo> orderVoList = new ArrayList<>();
        if (marketOrders != null) {
            PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
            CommonData data = CommonData.data(pageInfo);
            for (MarketOrder marketOrder : marketOrders) {
                MarketOrderGoodsExample example1 = new MarketOrderGoodsExample();
                OrderVo orderVo = new OrderVo();
                example1.createCriteria().andOrderIdEqualTo(marketOrder.getId());
                List<MarketOrderGoods> orderGoods = marketOrderGoodsMapper.selectByExample(example1);
                List<GoodsListVo> goodsListVos = new ArrayList<>();
                for (MarketOrderGoods orderGood : orderGoods) {
                    GoodsListVo goodsListVo = new GoodsListVo();
                    goodsListVo.setNumber(orderGood.getNumber());
                    goodsListVo.setPicUrl(orderGood.getPicUrl());
                    goodsListVo.setPrice(orderGood.getPrice());
                    goodsListVo.setId(orderGood.getId());
                    goodsListVo.setGoodsName(orderGood.getGoodsName());
                    goodsListVo.setSpecifications(orderGood.getSpecifications());
                    goodsListVos.add(goodsListVo);
                }
                orderVo.setGoodsList(goodsListVos);
                orderVo.setIsGroupin(false);

                HandleOption handleOption = new HandleOption();
                handleOption.setHandleOption(marketOrder.getOrderStatus());
                orderVo.setHandleOption(handleOption);

                orderVo.setAftersaleStatus(marketOrder.getAftersaleStatus());
                orderVo.setOrderSn(marketOrder.getOrderSn());
                orderVo.setActualPrice(marketOrder.getActualPrice());
                orderVo.setId(marketOrder.getId());
                if (marketOrder.getOrderStatus() == 101) {
                    orderVo.setOrderStatusText("待付款");
                }
                if (marketOrder.getOrderStatus() == 102) {
                    orderVo.setOrderStatusText("用户取消");
                }
                if (marketOrder.getOrderStatus() == 103) {
                    orderVo.setOrderStatusText("系统取消");
                }
                if (marketOrder.getOrderStatus() == 201) {
                    orderVo.setOrderStatusText("已付款");
                }
                if (marketOrder.getOrderStatus() == 202) {
                    orderVo.setOrderStatusText("申请退款");
                }
                if (marketOrder.getOrderStatus() == 203) {
                    orderVo.setOrderStatusText("已退款");
                }
                if (marketOrder.getOrderStatus() == 301) {
                    orderVo.setOrderStatusText("已发货");
                }
                if (marketOrder.getOrderStatus() == 401) {
                    orderVo.setOrderStatusText("已收货");
                }
                if (marketOrder.getOrderStatus() == 402) {
                    orderVo.setOrderStatusText("系统收货");
                }
                orderVoList.add(orderVo);
            }
            data.setList(orderVoList);
            return data;
        }
        return null;
    }

    @Autowired
    MarketAftersaleMapper aftersaleMapper;

    @Override
    public FrontOrderInfoVo detail(Integer orderId) {
        OrderInfo orderInfo = marketOrderMapper.selectOrderInfoByOrderId(orderId);
        String name = marketOrderMapper.selectChannelNameByCode(orderInfo.getExpCode());
        Short orderStatus = marketOrderMapper.getOrderStatusByOrderId(orderId);
        orderInfo.setExpName(name);
        orderInfo.setOrderStatusText(orderStatus);
        HandleOption handleOption = new HandleOption();
        handleOption.setHandleOption((orderStatus));
        orderInfo.setHandleOption(handleOption);

        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example);

        FrontOrderInfoVo frontOrderInfoVo = new FrontOrderInfoVo();
        String[] strings = {};
        frontOrderInfoVo.setExpressInfo(strings);
        if (orderInfo.getAftersaleStatus() == 0) {
            frontOrderInfoVo.setOrderInfo(orderInfo);
        }
        frontOrderInfoVo.setOrderGoods(marketOrderGoods);

        if (orderInfo.getAftersaleStatus() != 0) {
            FrontOrderInfoVo2 frontOrderInfoVo2 = new FrontOrderInfoVo2();
            MarketAftersaleExample example1 = new MarketAftersaleExample();
            example1.createCriteria().andOrderIdEqualTo(orderInfo.getId());
            List<MarketAftersale> marketAftersales = aftersaleMapper.selectByExample(example1);
            MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderInfo.getId());
            frontOrderInfoVo2.setAftersale(marketAftersales.get(0));
            frontOrderInfoVo2.setOrder(marketOrder);
            frontOrderInfoVo2.setOrderGoods(marketOrderGoods);
            return frontOrderInfoVo2;
        }

        return frontOrderInfoVo;
    }

    @Override
    public Integer selectUserIdByUsername(String username) {
        Integer userId = marketOrderMapper.selectUserIdByUserName(username);
        return userId;
    }

    @Override
    public int refund(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setOrderStatus(REFUND_STATUS);
        int i = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    @Override
    public int confirm(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setOrderStatus(CONFIRM_STATUS);
        int i = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    @Override
    public int delete(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setDeleted(true);
        int i = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    @Override
    public MarketOrderGoods goods(Integer orderId, Integer goodsId) {
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andGoodsIdEqualTo(goodsId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example);
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
            return marketOrderGood;
        }
        return null;
    }

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public int comment(OrderCommentBo orderCommentBo, Integer userId) {
        MarketComment marketComment = new MarketComment();
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        MarketOrderGoods marketOrderGoods1 = marketOrderGoodsMapper.selectByPrimaryKey(orderCommentBo.getOrderGoodsId());
        marketComment.setValueId(marketOrderGoods1.getGoodsId());
        marketComment.setContent(orderCommentBo.getContent());
        marketComment.setHasPicture(orderCommentBo.getHasPicture());
        marketComment.setPicUrls(orderCommentBo.getPicUrls());
        marketComment.setStar(orderCommentBo.getStar());
        marketComment.setUserId(userId);
        int i = marketCommentMapper.insertSelective(marketComment);
        if (i == 1) {
            MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
            marketOrderGoods.setId(orderCommentBo.getOrderGoodsId());
            marketOrderGoods.setComment(marketComment.getId());
            int i1 = marketOrderGoodsMapper.updateByPrimaryKeySelective(marketOrderGoods);
            if (i1 == 1) {
                return i;
            }
        }
        return 0;
    }

    @Override
    public int cancel(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setOrderStatus(CANCEL_STATUS);
        marketOrder.setEndTime(date);
        int i = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    @Override
    public int prepay(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setOrderStatus(PREPAY_STATUS);
        marketOrder.setPayTime(date);
        int i = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public int submit(Map map, MarketOrder marketOrder,List<Integer> goodsIds,List<Short> numbers,List<String[]> specificationss) {

        int i = marketOrderMapper.insertSelective(marketOrder);
        String msg = (String) map.get("msg");
        marketOrder.setMessage(msg);
        for (int i1 = 0; i1 < goodsIds.size(); i1++) {

            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsIds.get(i1));
            MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
            marketOrderGoods.setAddTime(date);
            marketOrderGoods.setUpdateTime(date);
            marketOrderGoods.setGoodsId(goodsIds.get(i1));
            marketOrderGoods.setOrderId(marketOrder.getId());
            marketOrderGoods.setGoodsName(marketGoods.getName());
            marketOrderGoods.setGoodsSn(marketGoods.getGoodsSn());
            marketOrderGoods.setProductId(marketGoods.getBrandId());
            marketOrderGoods.setNumber(numbers.get(i1));
            marketOrderGoods.setPrice(marketGoods.getRetailPrice());
            marketOrderGoods.setSpecifications(specificationss.get(i1));
            marketOrderGoods.setPicUrl(marketGoods.getPicUrl());
            marketOrderGoodsMapper.insertSelective(marketOrderGoods);
        }

        return i;
    }
}
